package com.ymttest.business.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ymatou.iapi.cardservice.parameter.AccountingItemModel;
import com.ymatou.iapi.cardservice.parameter.BizCodeEnum;
import com.ymatou.iapi.cardservice.parameter.FundoutEvidenceModel;
import com.ymatou.iapi.cardservice.parameter.UserCardModel;
import com.ymatou.iapi.cardservice.service.BindingCardCall;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.cardservice.CardServiceMapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.compare.AssertHelper;

public class CardCallService {
	private static CardServiceMapper cardServiceMapper = SqlMapperFactory.getCardServiceMapper();
	
	
	
	public static void bindingCard(String cardId,String cardPswd,int userId,String userName){
		cardServiceMapper.updateCardAsOrigByCardId(cardId);
		new BindingCardCall().call(cardPswd, userId, userName);
	}
	
	
	public static UserCardModel getUserCardModelFromDB(String cardId){
		Map<String,Object> cardMap = cardServiceMapper.selectCardByCardId(cardId);
		UserCardModel userCardModel = new UserCardModel();
		userCardModel.setCardId(cardId);
		userCardModel.setAvailAmount(new BigDecimal(String.valueOf(cardMap.get("AvailAmount"))));
		userCardModel.setDenomination(new BigDecimal(String.valueOf(cardMap.get("Denomination"))));
		userCardModel.setRemark(String.valueOf(cardMap.get("Remark")));
		userCardModel.setBeginTime((Date)cardMap.get("BeginTime"));
		userCardModel.setEndTime((Date)cardMap.get("EndTime"));
		userCardModel.setCardType((int)cardMap.get("CardType"));
		userCardModel.setCardUsableType(getCardUsableType(cardMap));
		return userCardModel;
	}
	
	public static void CheckUserCardModel(JSONObject ret, UserCardModel UserCardInDB){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		UserCardModel tgtModel = gson.fromJson(ret.toString(), UserCardModel.class);
		AssertHelper.assertResultEqual(tgtModel, UserCardInDB, "check user card model");
	}
	
	public static void CheckUserCardModel(JSONObject ret,String cardId){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		UserCardModel tgtModel = gson.fromJson(ret.toString(), UserCardModel.class);
		UserCardModel actModel = getUserCardModelFromDB(cardId);
		AssertHelper.assertResultEqual(tgtModel, actModel, "check user card model");
	}
	
	
	public static int getCardUsableType(Map<String,Object> cardMap) {
		int cardUsableType = 0;
		
		
		Date beginTime = (Date)cardMap.get("BeginTime");
		Date endTime = (Date)cardMap.get("EndTime");
		
		if (cardMap == null ||(int)cardMap.get("Status")!=5 || !(beginTime.before(new Date())&&endTime.after(new Date()))){
			cardUsableType= 1;
		}
		

		if (cardMap != null && BigDecimal.ZERO.compareTo( new BigDecimal(String.valueOf(cardMap.get("AvailAmount"))))==0 ){
			cardUsableType = 2;
		}
		
		return cardUsableType;
		
		
    }
	
	// 扣款model
	public static AccountingItemModel getPaymentAccountingItemModel(String cardId,int userId) {
		AccountingItemModel atm = new AccountingItemModel();
		atm.setCardId(cardId);
		atm.setUserId(userId);
		atm.setAmount(10);
		atm.setOperator(TestDataManager.randomStr(6));
		atm.setMemo(TestDataManager.randomStr(6));
		atm.setOriginalNo(TestDataManager.randomStr(10));
		atm.setBizNo(atm.getOriginalNo());
		atm.setBizCode(BizCodeEnum.Payment.getCode());
		atm.setAccountOperateType(2);
		return atm;
	}
	
	
	
	// 还款model
	public static AccountingItemModel getRefundAccountingItemModel(AccountingItemModel paymentModel) {
		AccountingItemModel atm = new AccountingItemModel();
		atm.setCardId(paymentModel.getCardId());
		atm.setUserId(paymentModel.getUserId());
		atm.setAmount(paymentModel.getAmount());
		atm.setOperator(TestDataManager.randomStr(6));
		atm.setMemo(TestDataManager.randomStr(6));
		atm.setOriginalNo(paymentModel.getOriginalNo());
		atm.setBizNo(TestDataManager.randomStr(10));
		atm.setBizCode(BizCodeEnum.Refund.getCode());
		atm.setAccountOperateType(1);
		
		FundoutEvidenceModel fem = new FundoutEvidenceModel();
		fem.setBizCode(BizCodeEnum.Payment.getCode());
		fem.setOriginalNo(paymentModel.getOriginalNo());
		fem.setBizNo(paymentModel.getBizNo());
		
		atm.setFundoutEvidence(fem);
		return atm;
	}
		
	
}
